package com.gt.controller.admin;

import com.gt.common.JwtProperties;
import com.gt.constant.JwtClaimsConstant;
import com.gt.context.BaseContext;
import com.gt.pojo.PageResult;
import com.gt.pojo.Result;
import com.gt.pojo.dto.EmployeeDTO;
import com.gt.pojo.dto.EmployeeLoginDTO;
import com.gt.pojo.dto.EmployeePageQueryDTO;
import com.gt.pojo.entity.Employee;
import com.gt.pojo.vo.EmployeeLoginVO;
import com.gt.service.EmployeeService;
import com.gt.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Api(tags = "管理员相关属性")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;


    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        Employee employee = employeeService.login(employeeLoginDTO);


        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
        claims.put("role",employee.getRole());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims
        );
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .role(employee.getRole())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    @ApiOperation("新增读者")
    @PostMapping
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        employeeService.save(employeeDTO);
        return Result.success();
    }

    @ApiOperation("读者分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }
    @ApiOperation("根据id查询员工")
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }
    @ApiOperation("编辑员工信息")
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }
    @GetMapping
    @ApiOperation("查询登录信息")
    public Result<Employee> userInfo(){
        Long currentId = BaseContext.getCurrentId();
        Employee employee = employeeService.getById(currentId);
        return Result.success(employee);
    }
    @DeleteMapping
    @ApiOperation("删除信息")
    public Result<String> deleteById(Long id){
        employeeService.deleteById(id);
        return Result.success();
    }
}
